import { defineConfig } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
import AutoImport from 'unplugin-auto-import/vite'
import { requestUrl, apiPrefix } from './src/config'

export default defineConfig(({ mode }) => ({
	plugins: [
		uni(),
		AutoImport({
			// 自动导入 Vue 相关函数，如：ref, reactive, toRef, onLoad 等
			imports: ['vue', 'vue-router', 'uni-app'],
			dts: './src/types/auto-imports.d.ts',
			vueTemplate: true,
			// 生成相应的.eslintrc-auto-import.json文件。
			eslintrc: {
				// 1、改为true 用于生成eslint配置。2、生成后改回false，避免重复生成消耗
				enabled: true
			}
		})
	],
	css: {
		preprocessorOptions: {
			scss: {
				additionalData: `@import "@/uni.scss"; @import "@/static/css/flex.scss";`
			}
		}
	},
	optimizeDeps: {
		exclude: ['sard-uniapp'],
	},
	build: {
		minify: 'terser',
		terserOptions: {
			compress: {
				// 生产环境去除log语句和DBUG
				drop_console: mode === 'production',
				drop_debugger: mode === 'production'
			}
		},
		// 配置静态资源路径为相对路径
		assetsDir: 'assets',
		rollupOptions: {
			output: {
				chunkFileNames: 'assets/[name]-[hash].js',
				entryFileNames: 'assets/[name]-[hash].js',
				assetFileNames: 'assets/[name]-[hash].[ext]'
			}
		}
	},
	// 静态资源处理配置
	publicDir: 'src/static',
	// 开发服务器跨域配置
	server: {
		port: 8085,

		strictPort: true,
		open: true,
		host: '0.0.0.0', // 允许局域网访问
		proxy: {
			[`/${apiPrefix}`]: {
				target: requestUrl,
				changeOrigin: true,
				rewrite: (path: string) => path.replace(new RegExp(`^\/${apiPrefix}`), '')
			}
		}
	}
}))